package stack_queue;

import java.util.Stack;

/**
 * @Author: wenjingyuan
 * @Date: 2022/09/28/9:46
 * @Description:  双栈实现最小栈
 *  https://leetcode-cn.com/problems/min-stack/
 */
public class MinStack {
    //具体存储元素
    private Stack<Integer> element=new Stack<>();
    //辅助栈一直用来存贮最小的元素
    private Stack<Integer> support=new Stack<>();
    public MinStack(){

    }
    public void push(int val){
        element.push(val);
        if(support.isEmpty()){
            support.push(val);
        }else{
            int tempMin=support.peek();
            //比较栈顶元素和val
            //将更小的放入栈中
            int min=Math.min(tempMin,val);
            support.push(min);
        }
    }
    public void pop(){
        support.pop();
        element.pop();
    }
    public int top(){
        return  element.peek();
    }
    public int getMin(){
        return support.peek();
    }
}
